package com.hugoo.rpt.busz;

import com.hugoo.ajxl.util.ExcelUtils;
import com.hugoo.core.Conf;
import com.hugoo.core.ConfigurationError;
import com.hugoo.core.db.ResultPack;
import com.hugoo.core.util.TL;
import com.hugoo.rpt.ExpPageInfo;
import com.hugoo.rpt.ParamError;
import com.hugoo.rpt.RptPageInfo;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Hugoo.Chan
 */
public class XlstExpAction extends RptPageInfo implements ExpPageInfo {

    public XlstExpAction(String sid, HttpServletRequest request) throws ParamError, ConfigurationError {
        super(sid, request);
    }

    @Override
    public String getFileName() {
        return getTitle() + ".xls";
    }

    private String getXlsTpl() {
        String st = Conf.getAutqProp2(_sid, Conf.RPT._rpt_xls_tpl);//
        return st;
    }

    private String fmt_init(String st) {
        if (st == null) {
            st = "";
        } else {
            st = "," + st + ",";
        }
        return st;
    }
    NumberFormat ccn = DecimalFormat.getNumberInstance();

    {
        ccn.setMaximumFractionDigits(2);
    }

    private Double prs(String vl) {
        if (TL.isEmpty(vl)) {
            return null;
        }
        String v = vl.replaceAll("[^0-9.-E]", "");
        return Double.parseDouble(v);
    }

    public String getFNUMColumns() {
        String st = Conf.getAutqProp2(_sid, Conf.RPT._rpt_xls_fnum_cols);//读取整数
        return this.fmt_init(st);
    }

    protected Double getDoubleValue(ResultSet rs, String key) throws SQLException {
        Object k = rs.getObject(key.toUpperCase());
        if (k == null) {
            return null;
        }
        if (k instanceof BigDecimal) {
            return ((BigDecimal) k).doubleValue();
        } else if (k instanceof String) {
            return TL.parseDouble(k.toString());
        }
        log.info("无效的数值类型！");
        return null;
    }

    @Override
    public ResultPack getResultPack() {
        return new ResultPack() {
            @Override
            public void packResult(ResultSet rs) {
                List<Map> lx = new ArrayList<Map>();
                Map m;
                FileOutputStream out = null;
                try {
                    /////////验证列是否有效///////////////
                    validateColumn(rs.getMetaData());
                    /////////////////////////
                    int c = col[0].length;
                    String fnum = getFNUMColumns();
                    boolean[] b = new boolean[col[0].length];
                    for (int z = 0; z < c; z++) {
                        b[z] = fnum.contains("," + col[0][z] + ",");
                    }
                    while (rs.next()) {
                        m = new HashMap();
                        for (int z = 0; z < c; z++) {
                            if (b[z]) {
                                m.put(col[0][z], getDoubleValue(rs, col[0][z]));
                            } else {
                                m.put(col[0][z], getValue(rs, col[0][z]));
                            }
                        }
                        lx.add(m);
                    }
                    m = new HashMap();
                    m.putAll(para);
                    m.put("rs", lx);
                    //#@!~~~~~~~~~~~~~~~~~W
                    out = new FileOutputStream(getTempFile());
                    String xtpl = getXlsTpl();
                    xtpl = XlstExpAction.class.getResource(xtpl).getPath();
                    log.info("取得模板文件路径为：" + xtpl);
                    ExcelUtils.export(xtpl, m, out);
                } catch (Exception ex) {
                    log.warn("处理Xls数据导出时，出现异常！", ex);
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException ex) {
                        }
                    }
                }
            }
        };
    }

    @Override
    protected String getDeclareColumnInfo() {
        return Conf.getAutqProp2(_sid, Conf.RPT._rpt_xls_columns);
    }
}
